<?php
/**
 * 框架运行支撑文件（加载类文件和生成框架缓存）
 * @copyright (c)2011-2012 php300.com
 * @license   dxtoo.cn
 * @author    lnbalife@126.com
 * @version   1.0beta
 */
class Runtime
{
	private $defines;
	private $configs;
	private $commons;
	private $coreClass;
	
	function __construct(){
		$this->getSysInfo();
		$this->init();
	}
	
	private function getSysInfo(){
		if(version_compare(PHP_VERSION,'6.0.0','<') ) {
		    @set_magic_quotes_runtime (0);
		    define('MAGIC_QUOTES_GPC',get_magic_quotes_gpc()?True:False);
		}
		define('MEMORY_LIMIT_ON',function_exists('memory_get_usage'));
		define('IN_CGI',substr(PHP_SAPI, 0,3)=='cgi' ? 1 : 0 );
		define('IN_WIN',strstr(PHP_OS, 'WIN') ? 1 : 0 );
		define('IN_CMD',PHP_SAPI=='cli'? 1 : 0);
	}
	
	public function checkRuntime(){
		if(!is_dir(RUNTIME_PATH)) mk_dir(RUNTIME_PATH);
		if(!is_writeable(RUNTIME_PATH)){
			exceptionMsg('目录[ '.RUNTIME_PATH.' ]不可写');
		}
		return true;
	}
	
	private function init(){
		$this->defines = array(
			GLOBAL_PATH.'defines.php',
		);
		$this->configs = array(
			GLOBAL_PATH.'baseconfig.php'
		);
		
		$this->commons = array(
			GLOBAL_PATH.'functions.php',
		);
		$this->coreClass = array(
			CX_PATH.'lib/core/Cxphp.class.php',
		    CX_PATH.'lib/core/Exception.class.php',
		    CX_PATH.'lib/core/Route.class.php',
		    CX_PATH.'lib/core/Module.class.php',
		    CX_PATH.'lib/core/App.class.php',
		    CX_PATH.'lib/core/Action.class.php',
		    CX_PATH.'lib/core/View.class.php',
		    CX_PATH.'lib/db/DB.class.php',
		    CX_PATH.'lib/db/dirver/Mysql.class.php',
		    CX_PATH.'lib/ext/Cookie.class.php',
		    CX_PATH.'lib/ext/Cache.class.php',
		    CX_PATH.'lib/ext/cache/Memcache.class.php',
		    CX_PATH.'lib/ext/cache/APC.class.php',
		    CX_PATH.'lib/ext/Log.class.php',
		    CX_PATH.'lib/ext/Template.class.php',
		    CX_PATH.'lib/ext/Widget.class.php',
		);
	}
	
	public function create(){
		//注意顺序
		$this->import($this->defines);
		$this->import($this->commons);
		$this->import($this->coreClass);
		
		if($this->configs)foreach ($this->configs as $key => $configfile){
			mc(include $configfile);
		}
		
		if(!defined('NO_CACHE_RUNTIME')){
			$this->checkRuntime();
			//写缓存
			$content = '';
			$content.=$this->import($this->defines,true);
			$content.=$this->import($this->commons,true);
			$content.=$this->import($this->coreClass,true);
			$runtimefile = '~runtime.php';
			if(defined('STRIP_RUNTIME_SPACE') && STRIP_RUNTIME_SPACE == false ) {
	            file_put_contents(RUNTIME_PATH.$runtimefile,'<?php'.$content);
	        }else{
	            file_put_contents(RUNTIME_PATH.$runtimefile,strip_whitespace('<?php'.$content));
	        }
	        unset($content);
		}
	}
	
	public function import($files,$return=''){
		$compile = defined('RUNTIME_MERGE');
		$content = '';
		if(is_array($files))foreach ($files as $key => $file) {
			if(is_file($file)){
				$return ? $content.=compile($file,$compile) : require $file;
			}
		}
		return $return ? $content:'';
	}
}
?>